package cn.ac.cintcm.prespring.etl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ShortText {
    /**
     * 固定长度的字符串切割
     * @param s 待切割字符串
     * @param width 切割的长度
     * @return
     */
    public static List<String> split(String s, int width) {
        List<String> strs = new ArrayList<String>();
        if (s != null && width > 0) {
            int i=0;
            while((i + width) < s.length()) {
                strs.add(s.substring(i, i+width));
                i = i + width;
            }
            if (i < s.length()) {
                strs.add(s.substring(i, s.length()));
            }
        }
        return strs;
    }
    
    /**
     * 固定长度的字符串递增切割
     * @param s 待切割字符串
     * @param width 切割的长度
     * @param increament 步距
     * @return
     */
    public static List<String> split(String s, int width, int increament) {
        List<String> strs = new ArrayList<String>();
        if (s != null && width > 0) {
            if (increament < 0 || increament > width) {
                increament = 1;
            }
            int i=0;
            while((i + width) < s.length()) {
                strs.add(s.substring(i, i+width));
                i = i + increament;
            }
            if (i < s.length()) {
                strs.add(s.substring(i, s.length()));
            }
        }
        return strs;
    }    
    /**
     * 将一串字符串按照非字符(!Character.isLetter())分割成字符串数组
     * @param alias 包含非字符的字符串
     * @return 不包含非字符串的字符串数组
     */
    public static List<String> splitNonChineseCharacter(String str) {
        List<String> words = null;
        if (str != null) {
            int length = str.length();
            words = new ArrayList<String>(length/3);
            int index = 0;
            while (index < length) {
                char[] buffer = new char[length - index];
                int bufferIndex = 0;
                while(index < length) {
                    if (!Character.isLetter(str.charAt(index))) {
                        index++;
                        break;
                    }
                    buffer[bufferIndex ++] = str.charAt(index);
                    index++;
                }
                if (bufferIndex > 0) {
                    words.add(new String(buffer, 0, bufferIndex));
                }
            }
        }
        return words;
    }
    
    public static HashMap<String, Integer> countText(List<String> texts) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        if (texts != null) {
            for (String s : texts) {
                Integer count = map.get(s);
                if (count == null) {
                    count = 1;
                } else {
                    count++;
                }
                map.put(s, count);
            }
        }
        return map;
    }

    public static List<String> splitNonChineseCharacter(List<String> stringSet) {
        List<String> strList = new ArrayList<String>();
        for(String s : stringSet) {
            if (s != null) {
                strList.addAll(splitNonChineseCharacter(s));
            }
        }
        return strList;
    }
    
    public static List<String> split(List<String> stringSet, int width) {
        List<String> strList = new ArrayList<String>();
        for(String s : stringSet) {
            if (s != null) {
                strList.addAll(split(s, width));
            }
        }
        return strList;
    }
    
    public static List<String> split(List<String> stringSet, int width, int increament) {
        List<String> strList = new ArrayList<String>();
        for(String s : stringSet) {
            if (s != null) {
                strList.addAll(split(s, width, increament));
            }
        }
        return strList;
    }
    
}
